<?php

/**
*  @author tungck
*  @filesource account.php
*  
*/


class Account extends CI_Controller{
    
    function __construct(){
        
        parent::__construct();
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
        $this->load->library('adminlayout');
        $this->adminlayout->setLayout("admin/layout");
        $userSession = $this->session->userdata('user');
        
        if($userSession['username'] == false){
            redirect(base_url('/index/login?returnUrl=').urlencode(RETURN_URL));
            
        }else{
            if($userSession['useradmin'] != 1){
                redirect(base_url('nhanvien/event/list_event'));
            }
        }
    }
    
    function listAccount(){
        
        $title = "Danh sách tài khoản";
        $this->adminlayout->view("admin/listAccount",null ,$title);
        
    }
    
    function getList(){
        $data['sort'] = $_GET["jtSorting"];
        $data['startIndex'] = $_GET["jtStartIndex"];
        $data['pageSize'] = $_GET["jtPageSize"];
        $this->load->model("admin/account_model");
        $result = $this->account_model->getList($data);
        $jTableResult = array();
		$jTableResult['Result'] = "OK";
        $jTableResult['TotalRecordCount'] = $result['recordCount'];
		$jTableResult['Records'] = $result['query'];
		print json_encode($jTableResult);
    }
    
    
    function addAccount(){
            if(isset($_POST['UserName']) && !empty($_POST['UserName'])){
                $data['UserName'] = $_POST['UserName'];
            }else{
                $err = "Thiếu tên tài khoản";
            }
            if(isset($_POST['TacNhan']) && !empty($_POST['TacNhan'])){
                $data['TacNhan'] = $_POST['TacNhan'];
            }
            if(isset($_POST['PassWord']) && !empty($_POST['PassWord'])){
                $data['PassWord'] = md5($_POST['PassWord']);
            }else{
                $err = "Thiếu mật khẩu";
            }
            if(isset($_POST['MaHoSo'])){
                $data['MaHoSo'] = $_POST['MaHoSo'];
            }else{
                $err = "Chọn mã hồ sơ";
            }
            $jTableResult = array();
            if(!isset($err)){
                $this->load->model("admin/account_model");
                $result = $this->account_model->insertAccount($data);
                $jTableResult['Result'] = "OK";
                $jTableResult['Record'] = $result;
            }else{
                $jTableResult['Result'] = "ERROR";
                $jTableResult['Message'] = array($err);
            }
            
		  print json_encode($jTableResult);
        
        
    }
    
    function deleteAccount(){
        try{
        if(isset($_POST["UserName"])){
            $user = $_POST["UserName"];
            $this->load->model("admin/account_model");
            $this->account_model->deleteAccount($user);
            $jTableResult = array();
            $jTableResult['Result'] = "OK";
            print json_encode($jTableResult);
            
        }
        }catch(Exception $ex){
            $jTableResult = array();
	        $jTableResult['Result'] = "ERROR";
	           $jTableResult['Message'] = $ex->message();
    	       print json_encode($jTableResult);
        }
        
    }
    
    function updateAccount(){
        
        if((isset($_POST['UserName'])) && ($_POST['UserName'] != "")){
                $data['UserName'] = $_POST['UserName'];
            }else{
                $err = "Thiếu tên tài khoản";
            }
            if(isset($_POST['TacNhan'])){
                $data['TacNhan'] = $_POST['TacNhan'];
            }
            if((isset($_POST['PassWord'])) && ($_POST['PassWord'] != "")){
                $data['PassWord'] = md5($_POST['PassWord']);
            }else{
                $err = "Thiếu mật khẩu";
            }
            if((isset($_POST['MaHoSo'])) && ($_POST['MaHoSo'] != "")){
                $data['MaHoSo'] = $_POST['MaHoSo'];
            }else{
                $err = "Chọn mã hồ sơ";
            }
            $jTableResult = array();
            if(!isset($err)){
                $this->load->model("admin/account_model");
                $result = $this->account_model->updateAccount($data);
                $jTableResult['Result'] = "OK";
                $jTableResult['Record'] = $result;
            }else{
                $jTableResult['Result'] = "ERROR";
                $jTableResult['Message'] = "$err";
            }
            
		  print json_encode($jTableResult);
    }
    
    
    
}

?>